package org.chen.controller;

import lombok.extern.slf4j.Slf4j;
import org.chen.common.exception.ServiceException;
import org.chen.domain.dto.*;
import org.chen.domain.response.PageResult;
import org.chen.domain.response.Result;
import org.chen.domain.vo.*;
import org.chen.service.ArticleService;
import org.chen.service.TranslationService;
import org.chen.utils.excel.ExcelReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/article")
@Slf4j
public class ArticleController {
    
    @Autowired
    private ArticleService articleService;

    @Autowired
    private TranslationService translationService;

    @PostMapping("/import")
    public Result<ImportResultVO> importArticles(
            @RequestParam("file") MultipartFile file,
            @RequestParam("categoryId") String categoryId
    ) {
        // 使用 try-with-resources 自动关闭资源
        try (ExcelReader reader = new ExcelReader(file)) {
            List<ArticleImportDTO.Article> allArticles = new ArrayList<>();

            // 分批读取Excel文件
            while (reader.hasNext()) {
                List<ArticleImportDTO.Article> batch = reader.readBatch();
                allArticles.addAll(batch);
            }

            // 调用service处理导入
            return Result.success(articleService.importArticles(allArticles, categoryId));

        } catch (IOException e) {
            log.error("Failed to read file", e);
            throw new ServiceException("文件读取失败");
        }
    }

    @GetMapping("/list")
    public Result<PageResult<ArticleVO>> getArticleList(ArticleQueryDTO query) {
        return Result.success(articleService.getArticleList(query));
    }
    
    @GetMapping("/{id}")
    public Result<ArticleVO> getArticleDetail(@PathVariable String id) {
        return Result.success(articleService.getArticleDetail(id));
    }
    
    @PutMapping("/{id}/status")
    public Result<Void> updateStatus(
            @PathVariable String id,
            @RequestParam Integer status) {
        articleService.updateArticleStatus(id, status);
        return Result.success();
    }
    
    @DeleteMapping("/{id}")
    public Result<Void> deleteArticle(@PathVariable String id) {
        articleService.deleteArticle(id);
        return Result.success();
    }

    @DeleteMapping("/batch")
    public Result<Void> batchDeleteArticles(@RequestBody BatchDeleteDTO request) {
        articleService.batchDelete(request.getIds());
        return Result.success();
    }

    @PutMapping("/save")
    public Result<Void> updateArticle(@RequestBody ArticleUpdateDTO updateDTO) {
        articleService.updateArticle(updateDTO);
        return Result.success();
    }

    @PutMapping("/batch/save")
    public Result<BatchSaveResultVO> batchUpdateArticles(@RequestBody BatchArticleUpdateDTO batchDTO) {
        return Result.success(articleService.batchUpdateArticles(batchDTO.getArticles()));
    }

    @PostMapping("/translate")
    public Result<ArticleVO> translateArticle(
            @RequestParam("id") String id,
            @RequestParam("targetLanguage") String targetLanguage) {
        return Result.success(translationService.translateArticle(id, targetLanguage));
    }

    @PostMapping("/translate/batch")
    public Result<BatchTranslationResultVO> batchTranslateArticles(
            @RequestBody BatchTranslationDTO batchDTO) {
        return Result.success(translationService.batchTranslateArticles(
                batchDTO.getArticleIds(),
                batchDTO.getTargetLanguage(),
                batchDTO.isUpdateOriginal()));
    }

    @GetMapping("/translations/{id}")
    public Result<List<ArticleTranslationVO>> getArticleTranslations(@PathVariable String id) {
        return Result.success(translationService.getArticleTranslations(id));
    }
}